package com.whfc.wxmp.config;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySources;
import com.whfc.common.entity.DefaultProperties;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.spring.AppContextUtil;
import com.whfc.entity.dto.msg.PushMsgDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-10-12 11:12
 */
@Configuration
@NacosPropertySources(value = {
        @NacosPropertySource(dataId = "com.whfc.wxmp.properties", autoRefreshed = true),
        @NacosPropertySource(dataId = "com.whfc.common.properties", autoRefreshed = true)
})
public class WxmpConfig {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${spring.profiles.active}")
    private String profile;

    @Bean
    public RedisTemplate<String, PushMsgDTO> redisTemplate(RedisConnectionFactory connectionFactory) {
        Jackson2JsonRedisSerializer redisSerializer = new Jackson2JsonRedisSerializer(PushMsgDTO.class);
        RedisTemplate<String, PushMsgDTO> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(redisSerializer);
        return redisTemplate;
    }

    @Bean
    public FilePathConfig filePathConfig() {
        return new FilePathConfig("ms", profile);
    }

    @Bean
    public AppContextUtil getAppContextUtil() {
        return new AppContextUtil();
    }

    @Bean
    @ConfigurationProperties("whfc.default")
    public DefaultProperties defaultProperties() {
        return new DefaultProperties();
    }
}
